package com.hz.ruoyimqttconsume.handler;

import com.rabbitmq.client.Channel;
import com.ruoyi.system.api.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
@Slf4j
public class AlertConsumerService {

    @Autowired
    private EquipmentService equipmentService;

    @RabbitListener(queues = "alert.queue", concurrency = "10")
    public void handleAlertMessage(Map<String, Object> message, Channel channel, Message amqpMessage) throws IOException {
        String code = (String) message.get("code");
        String alertInfo = (String) message.get("alertInfo");
//        AgrAlertLinkResultVo agrAlertLinkResultVo = (AgrAlertLinkResultVo) message.get("agrAlertLinkResultVo");

        try {


            // 发送短信
            //equipmentService.snedMsg("15290628091", code);
            log.info("短信已发送: code={}, alertInfo={}", code, alertInfo);

            // 手动确认消息
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("处理消息失败: {}", message, e);
            // 拒绝消息并重新入队
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
